package org.example.marker.spring;

import cn.hutool.core.date.DateUtil;
import org.example.marker.config.AppConfig;
import org.example.marker.service.impl.BitsAllocator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author Wills
 * @date 2020/5/11
 */
@Configuration
public class BeanConfiguration {

    @Bean
    public BitsAllocator bitsAllocator(AppConfig appConfig) {
        BitsAllocator bitsAllocator = new BitsAllocator(appConfig.getAppBits(), appConfig.getTimeBits(), appConfig.getWorkerBits(), appConfig.getSeqBits());
        bitsAllocator.setEpochStr(appConfig.getEpochStr());
        bitsAllocator.setEpochSeconds(TimeUnit.MILLISECONDS.toSeconds(DateUtil.parse(appConfig.getEpochStr(),"yyyy-MM-dd").getTime()));
        return bitsAllocator;
    }

}
